/*
 * flag.cpp
 *
 *  Created on: 15.06.2012
 *      Author: Caffeine
 */



#include "material__flag.h"
#include <lampe/exception/Base.h>
#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>

namespace lampe{
namespace xml{

class MaterialFlagFactory:public IIrrXmlNode{
private:
	irr::video::E_MATERIAL_FLAG STRtoMaterialFlag(String s){
	s=lampe::str::toUpperCase(s);
	if (s=="WIREFRAME") return irr::video::EMF_WIREFRAME;
	if (s=="POINTCLOUD") return irr::video::EMF_POINTCLOUD;
	if (s=="GOURAUD_SHADING") return irr::video::EMF_GOURAUD_SHADING;
	if (s=="LIGHTING") return irr::video::EMF_LIGHTING;
	if (s=="ZBUFFER") return irr::video::EMF_ZBUFFER;
	if (s=="ZWRITE_ENABLE") return irr::video::EMF_ZWRITE_ENABLE;
	if (s=="BACK_FACE_CULLING") return irr::video::EMF_BACK_FACE_CULLING;
	if (s=="FRONT_FACE_CULLING") return irr::video::EMF_FRONT_FACE_CULLING;
	if (s=="BILINEAR_FILTER") return irr::video::EMF_BILINEAR_FILTER;
	if (s=="TRILINEAR_FILTER") return irr::video::EMF_TRILINEAR_FILTER;
	if (s=="ANISOTROPIC_FILTER") return irr::video::EMF_ANISOTROPIC_FILTER;
	if (s=="FOG_ENABLE") return irr::video::EMF_FOG_ENABLE;
	if (s=="NORMALIZE_NORMALS") return irr::video::EMF_NORMALIZE_NORMALS;
	if (s=="TEXTURE_WRAP") return irr::video::EMF_TEXTURE_WRAP;
	if (s=="ANTI_ALIASING") return irr::video::EMF_ANTI_ALIASING;
	if (s=="COLOR_MASK") return irr::video::EMF_COLOR_MASK;
	if (s=="COLOR_MATERIAL") return irr::video::EMF_COLOR_MATERIAL;
	return irr::video::EMF_WIREFRAME;
}
public:
	MaterialFlagFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("material:flag");
		return list;
	};

	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
	if (!node->getParent() || !node->getParent()->getTag()){
		GEN_ERROR("Can't set flag: material not set");
		return lampe::xml::STOP;
	}
	String flag_name=node->getAttString("name","");
	if (flag_name==""){
		GEN_ERROR("Can't set flag: name of flag is empty");
		return lampe::xml::STOP;
	}
	irr::video::E_MATERIAL_FLAG flag=STRtoMaterialFlag(flag_name);
	irr::video::SMaterial*mat=(irr::video::SMaterial*)node->getParent()->getTag();
	mat->setFlag(flag,node->getAttBool("value",false));
	return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe

void readMaterialFlag(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::MaterialFlagFactory(d);
	environment->registerHandler(h);
	h->drop();
}